<!--  -->
<template>
  <div class="">
    <el-card>
      <el-table
        :data="categoryListDatas.data"
        style="width: 100%; margin-bottom: 20px"
        row-key="cid"
        border
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="cid" label="序号" sortable width="180">
        </el-table-column>
        <el-table-column prop="title" label="分类名称" sortable width="180">
        </el-table-column>
        <el-table-column prop="img" label="分类图片" width="180">
          <template slot-scope="scope">
            <img
              :src="'http://localhost:3000' + scope.row.img"
              width="50"
              height="50"
              alt=""
            />
          </template>
        </el-table-column>
        <el-table-column prop="is_show" label="是否可用">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.is_show"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-text="开"
              inactive-text="关"
              active-value="1"
              inactive-value="0"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="primary" size="small">编辑</el-button>
            <el-button
              type="danger"
              size="small"
              @click="deleCategryFn(scope.row.cid)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        background
        :page-sizes="[5, 10, 15, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="categoryListDatas.count"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import { categoryListApi, categoryDeleApi } from "@/api/category";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  //ComponentName和文件名保持一致
  name: "ComponentName",
  data() {
    //这里存放数据
    return {
      categoryListDatas: [],
      page: 1,
      size: 5,
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    deleCategryFn(cid) {
      console.log(cid);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "残忍删除",
        cancelButtonText: "再想想",
        type: "warning",
      })
        .then(() => {
          categoryDeleApi({ cid: cid }).then((result) => {
            console.log(result);
            if (result.data.status == 200) {
              this.getCategoryListDatas();
            } else {
              this.$message({
                type: "warning",
                message: result.data.msg,
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async getCategoryListDatas() {
      let result = await categoryListApi({
        page: this.page,
        size: this.size,
      });
      console.log(result);
      this.categoryListDatas = result.data;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.page = 1;
      this.getCategoryListDatas();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.page = val;
      this.getCategoryListDatas();
    },
  },
  beforeCreate() {}, //生命周期 - 创建之前
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  beforeMount() {}, //生命周期 - 挂载之前
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.getCategoryListDatas();
  },
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
  deactivated() {}, //如果页面有keep-alive缓存功能，当组件被移除时，触发钩子函数deactivated
};
</script>
<style>
</style>